package com.example.zyflutter.service;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.example.zyflutter.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class AppService {
  private final Context ctx;

  public AppService(Context context) {
    this.ctx = context;
  }

  /**
   * 获取应用列表
   *
   * @return
   */
  @SuppressLint("WrongConstant")
  public List<String> getPkgList() {
    LogUtils.INSTANCE.logE("AppService getPkgList");
    List<String> list = new ArrayList<>();
    try {
      List<ApplicationInfo> applicationInfos =
          ctx.getPackageManager()
              .getInstalledApplications(
                  PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES);
      for (ApplicationInfo applicationInfo : applicationInfos) {
        String pkgName = applicationInfo.packageName;
        list.add(pkgName);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return list;
  }
}
